
// 1. 大数相加
// 2. 大数相乘
// 3. 小数相加
// 4. 千分位分割 /(?=\B(\d{3})+$)/g
// 5. sum 递归求和
// 6. 红包算法
// 7. 10 进制转 26 进制/ 26 进制转 10 进制

function bigNumAdd(num1, num2) {
    let s1 = num1.toString().length
    let s2 = num2.toString().length
    let max = Math.max(s1, s2)
  
    let str1 = num1.toString().padStart(max, '0')
    let str2 = num2.toString().padStart(max, '0')
    let res = []
    let dig = 0
  
    for (let i = max - 1; i >= 0; i--) {
      let sum = parseInt(str1[i]) + parseInt(str2[i]) + dig
      res[i] = sum % 10
      dig = Math.floor(res[i] / 10)
    }
    return res.join('')
  }
  console.log(bigNumAdd(123, 2))
  
  
  function floatNumAdd(num1, num2) {
    let s1 = num1.toString().split('.')[1].length
    let s2 = num2.toString().split('.')[1].length
    let max = Math.max(s1, s2)
    let pow = Math.pow(10, max)
  
    let n1 = num1 * pow
    let n2 = num2 * pow
  
    return (n1 + n2) / pow
  }
  console.log(floatNumAdd(0.23, 0.14))
  
  
  function bigNumMutil(num1, num2) {
    let s1 = num1.toString().length;
    let s2 = num2.toString().length;
  
    let str1 = num1.toString();
    let str2 = num2.toString();
    let res = [];
    for (let i = s1 - 1; i >= 0; i--) {
      for (let j = s2 - 1; j >= 0; j--) {
        let m = i + j;
        let n = i + j + 1;
        let sum = str1[i] * str2[j] + (res[n] || 0)
        res[n] = sum % 10
        res[m] = Math.floor(sum / 10) + (res[m] || 0)
      }
    }
  
    return res.join('').replace(/^[0+]/g, '')
  }
  console.log(bigNumMutil(12, 28))
  
  
  function spiltNum(num) {
    let res = ''
  }
  
  
  function sum(arr) {
    function fn(i) {
      return i >= arr.length ? 0 : arr[i] + fn(i + 1)
    }
    return fn(0)
  }
  console.log(sum([1, 2, 3]))
  
  
  function redPackage(total, count, min) {
    let res = [] // 红包数量
    let remian = total // 剩余金额
  
    for (let i = 0; i < count; i++) {
      if (i === count - 1) {
        res.push(remian.toFixed(2))
      } else {
        let remianCount = count - i - 1
        let max = remian - (remianCount * min)
        let price = (Math.random() * (max - min) + min).toFixed(2)
        remian = remian - price
        res.push(price)
      }
    }
    return res;
  }
  console.log(redPackage(5, 5, 0.1))


  // 10 转 26 进制转换
function dToBase26(num){
    let base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  
    let res = "";
    while(num > 0){
      // 因为A代表1，所以需要将num减去1再进行模运算
      let index = (num - 1) % 26;
      res = base[index] + res;
      num = Math.floor((num - 1) / 26);
    }
    return res;
  }
  // 调用函数并输出结果
  console.log(dToBase26(1));
  console.log(dToBase26(26)); // 输出 z
  console.log(dToBase26(27)); // 输出 AA
  
  // 26 转 10
  function base26Tod(base26){
    let base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var res = 0;
  
    for(let i = 0; i< base26.length; i++){
      var digit = base.indexOf(base26[i].toUpperCase()) + 1; // 索引+1
      res = res * 26 + digit
    }
    return res
  }
  // 示例调用
  console.log(base26Tod('A'), '0000');
  console.log(base26Tod('Z'), '0000'); // 输出 26
  console.log(base26Tod('AA'), '0000'); // 输出 27